﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WEALTH.Manager.Customer
{
    public partial class AddChanpin : BasePage
    {
        ProductFilter product = new ProductFilter();
        ExcuteDao dao = new ExcuteDao();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void SetValue()
        {
            product = new ProductFilter();
            //产品全称
            product.CPName = SpecialCharHandler.HtmlEncode(txtCPName.Text);
            //发行机构
            product.CPFXJG = SpecialCharHandler.HtmlEncode(txtCPFXJG.SelectedItem.Text);
            //产品状态
            product.CPStauts = ddlCPStatus.SelectedValue;
            //产品类型
            product.CPType = SpecialCharHandler.HtmlEncode(txtCPType.SelectedItem.Text);
            //投资领域
            product.CPTZLY = SpecialCharHandler.HtmlEncode(txtCPTZLY.SelectedItem.Text);
            //产品期限
            product.CPQX = Convert.ToInt32(txtCPQX.Text);
            //发行规模
            product.CPFXGM = Convert.ToInt32(txtCPFXGM.Text);
            //投资起点
            product.CPQD = Convert.ToInt32(txtCPTZQD.Text);
            //预期年收益率
            product.CPSYFrom = txtCPSYFrom.Text;
            product.CPSYTo = txtCPSYTo.Text;
            //产品亮点
            product.CPLD = SpecialCharHandler.HtmlEncode(txtCPLD.Text);
            //收益率说明
            product.CPSYLSM = SpecialCharHandler.HtmlEncode(txtCPSYLSM.Text);
            //资金运用
            product.CPZJYY = SpecialCharHandler.HtmlEncode(txtCPZJYY.Text);
            //风险控制
            product.CPFXKZ = SpecialCharHandler.HtmlEncode(txtCPFXKZ.Text);
            //其他说明
            product.CPabout = SpecialCharHandler.HtmlEncode(txtCPAbout.Text);
            //付息方式
            product.CPFXFS = SpecialCharHandler.HtmlEncode(txtCPFXFS.Text);
            //发售日期
            product.CPFSRQ = txtCPFSRQ.Text;
            //成立日
            product.CPCLR = txtCPCLR.Text;
            //到期日
            product.CPDQR = txtCPDQR.Text;
            //添加人
            product.CPAddUser = getCurrentUser().UserID;

            if (cbxisall.Checked)
            {
                product.CPIsall = "ok";
            }

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //验证页面数据
                if (!CheckData())
                {
                    return;
                }
                SetValue();

                if (dao.Insert_Product(product) == 1)
                {
                    ReturnList("ChanpinList.aspx", "添加产品信息成功！");

                }
                else
                {
                    ShowMsg("添加产品信息失败，请确认！", 1);
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message.ToString(), 1);
            }
        }

        /// <summary>
        /// 验证页面内容
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtCPName.Text.TrimEnd()))
            {
                ShowMsg("请输入产品全称！", 1);
                txtCPName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCPFXJG.SelectedItem.Text.TrimEnd()))
            {
                ShowMsg("请选择发行机构！", 1);
                txtCPFXJG.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCPType.SelectedItem.Text.TrimEnd()))
            {
                ShowMsg("请选择产品类型！", 1);
                txtCPType.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCPTZLY.SelectedItem.Text.TrimEnd()))
            {
                ShowMsg("请选择投资领域！", 1);
                txtCPTZLY.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCPQX.Text.TrimEnd()))
            {
                ShowMsg("请输入产品期限！", 1);
                txtCPQX.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCPFXGM.Text.TrimEnd()))
            {
                ShowMsg("请输入发行规模！", 1);
                txtCPFXGM.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCPTZQD.Text.TrimEnd()))
            {
                ShowMsg("请输入投资起点！", 1);
                txtCPTZQD.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCPSYFrom.Text.TrimEnd()))
            {
                ShowMsg("请输入预期年收益率！", 1);
                txtCPSYFrom.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtCPSYTo.Text.TrimEnd()))
            {
                ShowMsg("请输入预期年收益率！", 1);
                txtCPSYTo.Focus();
                return false;
            }
            if (Convert.ToDouble(txtCPSYFrom.Text) > Convert.ToDouble(txtCPSYTo.Text))
            {
                ShowMsg("预期年收益率输入不正确，请确认！", 1);
                txtCPSYFrom.Focus();
                return false;
            }          

            return true;
        }
    }
}